package core;

import java.util.ArrayList;
import java.util.List;

import observers.*;
import notifyers.*;

public class Calculator implements CalculatorAddNotifyer, CalculatorSubstractNotifyer
{
	public Calculator() {
		super();
		this.add_observers = new ArrayList<CalculatorAddObserver>();
		this.sub_observers = new ArrayList<CalculatorSubstractObserver>();
	}

	public List<CalculatorAddObserver> add_observers;
	public List<CalculatorSubstractObserver> sub_observers;

	public int add(int a, int b)
	{
		notify_add_observers(a,b);
		return a + b; 
	}
	public int substract(int a, int b)
	{
		notify_substraction_observers(a, b);
		return a - b; 
	}
	
	@Override
	public void register_add_observer(CalculatorAddObserver obs) {
		// TODO Auto-generated method stub
		System.out.println("Calculator " + this + " registering AddObserver " + obs);
		add_observers.add(obs);
	}
	@Override
	public void notify_add_observers(int a, int b) {
		// TODO Auto-generated method stub
		for( CalculatorAddObserver obs : add_observers)
		{
			System.out.println("Signaling Add observer : " + obs + " with " + a + " and " + b);
			obs.notify(this, a, b);
		}
	}
	@Override
	public void register_substraction_observer(CalculatorSubstractObserver obs) {
		// TODO Auto-generated method stub
		System.out.println("Calculator " + this + " registering SubstractObserver " + obs);
		sub_observers.add(obs);
	}
	@Override
	public void notify_substraction_observers(int a, int b) {
		// TODO Auto-generated method stub
		for( CalculatorSubstractObserver obs : sub_observers)
		{
			System.out.println("Signaling Sub observer : " + obs + " with " + a + " and " + b);
			obs.notify(this, a, b);
		}
	}
}
